﻿
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GetStaffInPosition_V2]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_GetStaffInPosition_V2];
GO
CREATE PROCEDURE [dbo].[sproc_GetStaffInPosition_V2]
    @Position_ID int,
    @Dimission bit = 0
/*

============================================================
功能:    得到职位人员
参数:
    @Position_ID int    :    职位ID (指定-1表示所有职位）
    @Dimission bit      :    是否在职

============================================================

*/
AS
BEGIN
SET NOCOUNT ON

IF ( @Position_ID <> -1 )
BEGIN
    -- 获取指定职位的所有员工
    SELECT S.*,
            P.Position_ID,
            P.Position_name
        FROM
            dbo.UDS_Staff S
            INNER JOIN dbo.UDS_Staff_In_Position SIP ON S.Staff_ID = SIP.Staff_ID
            INNER JOIN dbo.UDS_Position P ON SIP.Position_ID = P.Position_ID
        WHERE
            S.Dimission = @Dimission
            AND P.Position_ID = @Position_ID
        ORDER BY
            S.RegistedDate DESC;
END
ELSE
BEGIN
    -- 获取系统内的所有员工
    SELECT S.*,
            P.Position_ID,
            P.Position_name
        FROM
            dbo.UDS_Staff S
            INNER JOIN dbo.UDS_Staff_In_Position SIP ON S.Staff_ID = SIP.Staff_ID
            INNER JOIN dbo.UDS_Position P ON SIP.Position_ID = P.Position_ID
        WHERE
            S.Dimission = @Dimission
        ORDER BY
            S.RegistedDate DESC;
END

END
GO
